package com.IntelligentCommunity.rabbitmq.consumer;

import com.IntelligentCommunity.common.core.domain.R;
import com.IntelligentCommunity.common.core.utils.StringUtils;
import com.IntelligentCommunity.deploy.api.DeployAlarmRemote;
import com.IntelligentCommunity.deploy.api.WebSocketRemote;
import com.IntelligentCommunity.deploy.domain.DeployAlarm;
import com.IntelligentCommunity.deploy.domain.DeployTaskFace;
import com.IntelligentCommunity.deploy.domain.vo.AlarmFace;
import com.IntelligentCommunity.rabbitmq.utils.EmailUtil;
import com.IntelligentCommunity.rabbitmq.utils.MsgUtil;
import com.IntelligentCommunity.system.api.RemoteUserService;
import com.IntelligentCommunity.system.domain.vo.OpFinUser;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@Log4j2
public class MessageListener {
    // 日志记录器
    @Autowired
    private DeployAlarmRemote deployAlarmRemote;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private WebSocketRemote webSocketRemote;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private MsgUtil msgUtil;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("queue7"),
            exchange = @Exchange(value = "fanoutExchange", type = ExchangeTypes.FANOUT)
    ))
    public void publisherSubscribeConsumer1(String deployAlarmJson) {
        // TODO 添加记录
        if (StringUtils.isBlank(deployAlarmJson)){
            log.error("消息为空");
            throw new RuntimeException("消息为空");
        }
        AlarmFace alarmFace = JSONObject.parseObject(deployAlarmJson, AlarmFace.class);
        DeployAlarm deployAlarm = alarmFace.getDeployAlarm();
        DeployTaskFace deployTaskFace = alarmFace.getDeployTaskFace();
        if (StringUtils.isNull(deployAlarm)){
            throw new RuntimeException("添加对象为空");
        }
        deployAlarm.setTaskId(deployTaskFace.getId());
        deployAlarmRemote.alarmAdd(deployAlarm);  // 添加告警到数据库
        log.info("消息的消费者1接受到消息，消息内容：{}", deployAlarmJson);

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("queue8"),
            exchange = @Exchange(value = "fanoutExchange", type = ExchangeTypes.FANOUT)
    ))
    public void publisherSubscribeConsumer2(String deployAlarmJson) {
        // 告警 发邮件 发短信
            log.info("消息的消费者2接受到消息，消息内容：{}", deployAlarmJson);
            if (StringUtils.isBlank(deployAlarmJson)) {
                throw new RuntimeException("消息为空");
            }
            AlarmFace alarmFace = JSONObject.parseObject(deployAlarmJson, AlarmFace.class);
            if (StringUtils.isNull(alarmFace)) {
                throw new RuntimeException("消息对象为空");
            }
            DeployTaskFace deployTaskFace = alarmFace.getDeployTaskFace();
            if (null == deployTaskFace.getUserId()) {
                throw new RuntimeException("找不到创建用户用户id为空");
            }
            R<OpFinUser> infoById = remoteUserService.getInfoById(deployTaskFace.getUserId());
            if (infoById.getCode() != 200) {
                throw new RuntimeException("找不到创建用户");
            }
            OpFinUser data = infoById.getData();
            emailUtil.sendCodeByEmail("发现事件" + deployTaskFace.getTaskDesc() + "嫌疑人", data.getEmail()); // 发送邮件
            msgUtil.sendMsg(data.getTel(),"8888"); // 发送短信
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("queue9"),
            exchange = @Exchange(value = "fanoutExchange", type = ExchangeTypes.FANOUT)
    ))
    public void publisherSubscribeConsumer3(String deployAlarmJson) {
        log.info("消息的消费者3接受到消息，消息内容：{}", deployAlarmJson);
        webSocketRemote.sendWran(deployAlarmJson);
    }
}
